import { ref, computed } from 'vue'

// 表单间隔
export const formItemLayout = {
  labelCol: {
    width: '150px'
  }
}

export const form = [
  {
    title: '名字',
    key: 'name',
    type: 'input'
  }
]

export const data = [
  {
    department: '人事部',
    project: '1.演练目标',
    content: '演练目标合理、具体、可执行且具有针对性，符合现场实际'
  },
  {
    department: '人事部',
    project: '2.内容描述',
    content: '演练方案文字通顺、语言精炼、通俗易懂'
  },
  {
    department: '人事部',
    project: '3.角色分工',
    content: '演练方案中角色的分工明确，职责清晰，人数满足演练要求'
  },
  {
    department: '开发部',
    project: '4.演练场地',
    content: '演练场地选择符合演练策划情景设置要求，现场条件满足演练要求'
  },
  {
    department: '开发部',
    project: '5.物质准备',
    content: '应急设施、设备、器材等数量和性能能够满足现场应急需要'
  },
  {
    department: '采购部',
    project: '6.组织安排',
    content: '应急指挥人员能否迅速组织应急队伍，并迅速安排任务'
  },
  {
    department: '采购部',
    project: '7.指令表达',
    content: '应急指挥人员指令是否表达清晰、简洁、完整、准确'
  },
  {
    department: '采购部',
    project: '8.反应速度',
    content: '参演人员能否按照演练方案规定迅速达到现场开展救援'
  },
  {
    department: '采购部',
    project: '9.防护装备',
    content: '参演人员是否配备适当的个体防护装备，或采取了必要自我安全防护措施'
  },
  {
    department: '采购部',
    project: '10.报告反馈',
    content:
      '参演人员事故报告是否快速、清晰、准确，及时传达各项指令和反馈现场信息'
  },
  {
    department: '销售部',
    project: '11.流畅程度',
    content: '参演人员能是否按照演练方案流畅完成各演练环节。'
  },
  {
    department: '销售部',
    project: '12.方案可执行性',
    content: '通过演练是否全面、彻底检验了演练方案的合理性、可执行性'
  },
  {
    department: '销售部',
    project: '13.参演人员熟练性',
    content: '通过演练是否让参演人员对应急处置流程有了明确的了解与掌握'
  },
  {
    department: '财务部',
    project: '14.应急能力锻炼',
    content: '通过演练是否有效提高了参演人员的应急能力'
  },
  {
    department: '财务部',
    project: '15.应急物质合理性',
    content: '通过演练是否对应急物质的数量、性能等有合理性判断'
  }
]

// 计算每个字段的合并信息
const mergeInfo = computed(() => {
  const info = {}
  // 需要合并的字段
  const fields = ['department']
  fields.forEach((field) => {
    info[field] = {}
    let currentValue = null
    let startIndex = 0
    let count = 0

    data.forEach((item, index) => {
      const value = item[field] || ''

      if (value !== currentValue) {
        if (count > 0) {
          info[field][startIndex] = count
        }

        currentValue = value
        startIndex = index
        count = 1
      } else {
        count++
      }
    })

    // 处理最后一组
    if (count > 0) {
      info[field][startIndex] = count
    }
  })

  return info
})

// 获取单元格的rowSpan
const getRowSpan = (field, index) => {
  for (const startIndex in mergeInfo.value[field]) {
    const span = mergeInfo.value[field][startIndex]
    if (index >= parseInt(startIndex) && index < parseInt(startIndex) + span) {
      return index === parseInt(startIndex) ? span : 0
    }
  }
  return 1
}
// 动态列定义
export const columns = [
  // {
  //   width: 70,
  //   title: '序号',
  //   dataIndex: 'index',
  //   key: 'index'
  // },
  {
    colSpan: 2,
    title: '部门',
    dataIndex: 'department',
    key: 'department',
    customCell: (record, index) => {
      const rowSpan = getRowSpan('department', index)
      return { rowSpan }
    }
  },
  {
    colSpan: 0,
    title: '评估项目',
    dataIndex: 'project',
    key: 'project'
  },
  {
    title: '内容',
    dataIndex: 'content',
    key: 'content'
  }
]
